---
title: "Replication: Does Mainstream Populism Work?"
author: "Markus Kollberg"
date: "`r Sys.Date()`"
output: html_document
---

```{r setup, include=TRUE, message = FALSE}
# Load required libraries
library(haven)
library(tidyverse)
library(ggplot2)
library(texreg)
library(gridExtra)
library(interplot)
library(sjPlot)
library(ggsignif)

```

## Load data and create subfolders for storing plots and tables

```{r data, include=TRUE, message = FALSE}
data_cleaned <- read_csv("data_replication_271024.csv")
dir.create(file.path("Tables"))
dir.create(file.path("Plots"))
```

## Comparison between all four treatment groups

```{r main analysis, include=TRUE, message = FALSE}
main_fit <- lm(party_eval ~ position + rhetoric + rhetoric*position + issue + party, data = data_cleaned)
summary(main_fit) 

### Produce Table 2

texreg(list(main_fit),
       file = "Tables/main_model_271024.tex",
       include.ci = FALSE,
       stars = c(0.001, 0.01, 0.05),
       custom.coef.map = list("(Intercept)" = "No Accommodation (Intercept)",
                              "rhetoric" = "Rhetorical Accommodation",
                              "position" = "Positional Accommodation ",
                              "position:rhetoric" = "Rhetorical Acc. * Positional Acc."),
       booktabs = TRUE,
       dcolumn = FALSE,
       custom.model.names = c("Main Model"),
       custom.note = "%stars. Controlling for Issue and Mainstream Party",
       digits = 2,
       label = "Main Model")

### Conduct t-tests

# Groups 1 and 2
t.test(data_cleaned$party_eval ~ data_cleaned$treat_cat, subset = data_cleaned$treat_cat == 1 | data_cleaned$treat_cat == 2)

# Groups 1 and 3
t.test(data_cleaned$party_eval ~ data_cleaned$treat_cat, subset = data_cleaned$treat_cat == 1 | data_cleaned$treat_cat == 3)

# Groups 1 and 4
t.test(data_cleaned$party_eval ~ data_cleaned$treat_cat, subset = data_cleaned$treat_cat == 1 | data_cleaned$treat_cat == 4)

## Produce Figure 1

data_main <- data_cleaned %>% group_by(treat_cat) %>% summarise(m = mean(party_eval),
                                                                   SE = sd(party_eval)/sqrt(length(party_eval)))

plot_main <- ggplot(data_main, aes(treat_cat,m,fill=as.factor(treat_cat))) + 
  geom_point(stat="identity", color="black",
           position=position_dodge())+
  geom_errorbar(aes(ymin=m-1.96*SE, ymax=m+1.96*SE), width=.5,
                position=position_dodge(.9))+
  theme_classic()+
  theme(legend.position = "none")+
  labs(title = "Means for different Treatment Categories")+
  xlab("Treatment Category")+
  ylab("Propensity to Vote for Mainstream Party (-5 to 5)")+
  scale_x_continuous(
    breaks = c(1, 2, 3, 4),
    label = c("No accommodation", "Positional \n Accommodation", "Rhetorical \n Accommodation", "Full \n Accommodation"))+
  geom_signif(comparisons = list(c(1, 2)), annotations = "Not significant" , y_position = 0.05)+
  geom_signif(comparisons = list(c(1, 4)), annotations = "Not significant", y_position = 0.4)+
  geom_signif(comparisons = list(c(1, 3)), annotations = "Not significant", y_position = 0.2)+
  theme(text = element_text(size = 14))+
  geom_hline(yintercept =0, linetype = 2)+
  theme(text = element_text(size = 14))
  
plot_main

ggsave("Plots/plot_main_271024.png", plot_main, width = 260, height = 140, device='png', units = "mm", dpi=300, scale = 1.3)


```

## Moderation by respondents' issue preferences

```{r preferences, include=TRUE, message = FALSE}

prefs_fit1 <- lm(party_eval ~ position + rhetoric + position*rhetoric + pref_directed + issue + party, data = data_cleaned)
summary(prefs_fit1)

prefs_fit2 <- lm(party_eval ~ position*pref_directed + rhetoric + position*rhetoric + issue + party, data = data_cleaned)
summary(prefs_fit2) 

prefs_fit3 <- lm(party_eval ~ position*pref_directed + rhetoric*pref_directed + position*rhetoric + issue + party, data = data_cleaned)
summary(prefs_fit3) 

prefs_fit4 <- lm(party_eval ~ position*pref_directed + rhetoric*pref_directed + position*rhetoric*pref_directed + issue + party, data = data_cleaned)
summary(prefs_fit4) 

### Appendix Table 5 

texreg(list(prefs_fit1, prefs_fit2, prefs_fit3, prefs_fit4),
       file = "Tables/prefs_models_271024.tex",
       include.ci = FALSE,
       stars = c(0.001, 0.01, 0.05),
       custom.coef.map = list("(Intercept)" = "No accommodation (Intercept)",
                              "rhetoric" = "Rhetorical Accommodation",
                              "position" = "Position Accommodation",
                              "position:rhetoric" = "Rhetorical Acc. * Positional Acc.",
                              "pref_directed" = "Respondent LR-Preferences",
                              "pref_directed:rhetoric" = "Respondent LR-Preferences * Rhetorical Acc.",
                              "position:pref_directed" = "Respondent LR-Preferences * Positional Acc.",
                              "position:pref_directed:rhetoric" = "Respondent LR-Preferences * Rhetorical Acc. * Positional Acc."),
       booktabs = TRUE,
       dcolumn = FALSE,
       custom.note = "%stars. Controlling for Issue and Mainstream Party",
       digits = 2,
       label = "Effects conditional on Voters' Left-Right Preferences")


## Manuscript Table 2 

texreg(list(main_fit, prefs_fit4),
       file = "Tables/main_models_271024.tex",
       include.ci = FALSE,
       stars = c(0.001, 0.01, 0.05),
       custom.coef.map = list("(Intercept)" = "No accommodation (Intercept)",
                              "rhetoric" = "Rhetorical Accommodation",
                              "position" = "Positional Accommodation",
                              "position:rhetoric" = "Rhetorical Acc. * Positional Acc.",
                              "pref_directed" = "Respondent LR-Preferences",
                              "pref_directed:rhetoric" = "Respondent LR-Preferences * Rhetorical Acc.",
                              "position:pref_directed" = "Respondent LR-Preferences * Positional Acc.",
                              "position:pref_directed:rhetoric" = "Respondent LR-Preferences * Rhetorical Acc. * Positional Acc."),
       booktabs = TRUE,
       dcolumn = FALSE,
       custom.note = "%stars. Controlling for Issue and Mainstream Party",
       digits = 2,
       caption  = "The average effects of both accommodation strategies and effects conditional on respondent LR-preferences")


```

## Plot predicted values with discrete binning (Figure 2)

```{r plot moderation,  include=TRUE, message = FALSE}

### turn into factor for discrete binning
data_cleaned$prefs_fac <- as.factor(data_cleaned$pref_directed)
data_cleaned$rhetoric_name <- ifelse(data_cleaned$rhetoric == 1, "Rhetorical Accommodation", "No Rhetorical Accommodation")
data_cleaned$position_name <- ifelse(data_cleaned$position == 1, "Positional Accommodation", "No Positional Accommodation")

# re-run model for plotting with discrete binning
prefs_fit_name <- lm(party_eval ~ position_name*prefs_fac + rhetoric_name*prefs_fac + position_name*rhetoric_name*prefs_fac, data = data_cleaned)
summary(prefs_fit_name)

texreg(list(prefs_fit_name),
       file = "Tables/main_discrete_binning_271024.tex",
       include.ci = FALSE,
       stars = c(0.001, 0.01, 0.05),
       custom.coef.names = c("Intercept", 
                             "Positional Accommodation",  # Dummy variable for position_name
                             "LR-Preferences -1",      # Dummy variable for prefs_fac level 2
                             "LR-Preferences 0",      # Dummy variable for prefs_fac level 3
                             "LR-Preferences 1",      # Dummy variable for prefs_fac level 4
                             "LR-Preferences 2",      # Dummy variable for prefs_fac level 5
                             "Rhetorical Accommodation",     # Coefficient for rhetoric_name
                             "Positional Accommodation * LR-Preferences -1",  # Interaction 
                             "Positional Accommodation * LR-Preferences  0 ",
                             "Positional Accommodation * LR-Preferences 1 ",
                             "Positional Accommodation * LR-Preferences 2 ",
                             "Rhetorical Accommodation * LR-Preferences -1",  # Interaction 
                             "Rhetorical Accommodation * LR-Preferences  0 ",
                             "Rhetorical Accommodation * LR-Preferences 1 ",
                             "Rhetorical Accommodation * LR-Preferences 2 ",
                             "Positional * Rhetorical Acc.",  # Main effect interaction
                             "Positional * Rhetorical Acc. * LR-Preferences -1",
                             "Positional * Rhetorical Acc. * LR-Preferences 0",
                             "Positional * Rhetorical Acc. * LR-Preferences 1",
                             "Positional * Rhetorical Acc. * LR-Preferences 2"),
       booktabs = TRUE,
       dcolumn = FALSE,
       custom.note = "%stars. Controlling for Issue and Mainstream Party",
       digits = 2,
       caption  = "The average effects of both accommodation strategies and effects conditional on respondent LR-preferences (discrete binning)")


#plot rhetoric
rhet_main_plot <- plot_model(prefs_fit_name, type = "eff",
                           terms = c("prefs_fac", "rhetoric_name"),
                           title = "Predicted Values for Mainstream Party Rhetoric",
                           axis.title = c("Respondent Preferences \n Left-Right Scale", "\n Predicted Propensity to Vote for Mainstream Party (-5 to 5)"),
                          legend.title = "Rhetoric shown:",
                           ci.lvl = .95,
                          dodge = .2,
                          colors = c("gray60", "black"),
                          dot.size = 3,
                          line.size = 1.3) + 
                            theme_classic()+
                            geom_histogram(data = data_cleaned, 
                            aes(x = pref_directed, y = (..count../1000)),
                            fill = "white", colour = "gray95", alpha = 0)+
  scale_y_continuous(
        name = "Predicted Propensity to Vote for Mainstream Party (-5 to 5)",
        sec.axis = sec_axis(~.*1000, name="Distribution of Preferences among Respondents", breaks = c(0,1000, 2000, 3000))
  )+
  theme(text = element_text(size = 14), legend.position = "bottom")+
  geom_hline(yintercept = 0.0, linetype = 1)

# plot positions
pos_main_plot <- plot_model(prefs_fit_name, type = "eff",
                             terms = c("prefs_fac", "position_name"),
                             title = "Predicted Values for Mainstream Party Positions",
                             axis.title = c("Respondent Preferences \n Left-Right Scale", "Predicted Propensity to Vote for Mainstream Party (-5 to 5)"),
                             legend.title = "Position shown:",
                             ci.lvl = .95,
                             dodge = .2,
                             colors = c("gray60", "black"),
                             dot.size = 3,
                             line.size = 1.3) + 
  theme_classic()+
  geom_histogram(data = data_cleaned, 
                 aes(x = pref_directed, y = (..count../1000)),
                 fill = "white", colour = "gray95", alpha = 0)+
  scale_y_continuous(
    name = "Predicted Propensity to Vote for Mainstream Party (-5 to 5)",
    sec.axis = sec_axis(~.*1000, name=" \n Distribution of Preferences among Respondents", breaks = c(0,1000, 2000, 3000))
  )+
  theme(text = element_text(size = 14), legend.position = "bottom")+
  geom_hline(yintercept = 0.0, linetype = 1)


main_figure <- grid.arrange(pos_main_plot, rhet_main_plot, ncol = 2)

ggsave("Plots/main_figure_271024.png", main_figure, width = 285, height = 160, device='png', units = "mm", dpi=300, scale = 1.2)



```

# Manipulation Checks

```{r plot manipulation,  include=TRUE, message = FALSE}

mani_lang <- lm(post_lang ~ position + rhetoric + rhetoric*position, data = data_cleaned)
summary(mani_lang)

#t-tests 
### conditions 1 and 3
t.test(data_cleaned$post_lang ~ data_cleaned$treat_cat, subset = data_cleaned$treat_cat == 1 | data_cleaned$treat_cat == 3)
### 1 and 2
t.test(data_cleaned$post_lang ~ data_cleaned$treat_cat, subset = data_cleaned$treat_cat == 1 | data_cleaned$treat_cat == 2)
### 2 and 4
t.test(data_cleaned$post_lang ~ data_cleaned$treat_cat, subset = data_cleaned$treat_cat == 2 | data_cleaned$treat_cat == 4 )
### 2 and 3
t.test(data_cleaned$post_lang ~ data_cleaned$treat_cat, subset = data_cleaned$treat_cat == 2 | data_cleaned$treat_cat == 3 )


data_man_lang <- data_cleaned %>% group_by(treat_cat) %>% summarise(m = mean(post_lang),
                                       SE = sd(post_lang)/sqrt(length(post_lang)))

plot_man_rhet<- ggplot(data_man_lang, aes(treat_cat,m,fill=as.factor(treat_cat))) + 
  geom_point(stat="identity", color="black",
              position=position_dodge())+
  geom_errorbar(aes(ymin=m-1.96*SE, ymax=m+1.96*SE), width=.5,
                position=position_dodge(.9))+
  scale_fill_manual(values=c("#B2BEB5",
                             "gray",
                             "darkgray",
                             "#71797E"))+
  theme_classic()+
  theme(legend.position = "none")+
  labs(title = "Manipulation Check for Mainstream Party Rhetoric")+
  xlab("Treatment Category")+
  ylab("Choice of Words resembles those of the AfD")+
  scale_x_continuous(
    breaks = c(1, 2, 3, 4),
    label = c("No accommodation", "Positional \n Accommodation", "Rhetorical \n Accommodation", "Full \n Accommodation"))+
  geom_signif(comparisons = list(c(1, 2)), annotations = "***", y_position = 3.1)+
  geom_signif(comparisons = list(c(1, 3)), annotations = "***", y_position = 3.3)+
  geom_signif(comparisons = list(c(3, 4)), annotations = "***", y_position = 3.5)+
  geom_signif(comparisons = list(c(2, 4)), annotations = "***", y_position = 3.7)+
  theme(text = element_text(size = 14))
  
mani_pos <- lm(post_pos ~ position + rhetoric + rhetoric*position, data = data_cleaned)
summary(mani_pos)

### Appendix table 3
texreg(list(mani_lang, mani_pos),
       file = "Tables/manipulation_checks_271024.tex",
       include.ci = FALSE,
       stars = c(0.001, 0.01, 0.05),
       custom.coef.map = list("(Intercept)" = "Constant",
                              "rhetoric" = "Rhetoric",
                              "position" = "Position",
                              "position:rhetoric" = "Rhetoric * Position"),
       booktabs = TRUE,
       dcolumn = FALSE,
       custom.model.names = c("Rhetoric", "Position"),
       custom.note = "%stars.",
       digits = 2,
       label = "Manipulation Checks for Rhetoric and Position")


#t-tests 
### conditions 1 and 3
t.test(data_cleaned$post_pos ~ data_cleaned$treat_cat, subset = data_cleaned$treat_cat == 1 | data_cleaned$treat_cat == 3)
### 1 and 2
t.test(data_cleaned$post_pos ~ data_cleaned$treat_cat, subset = data_cleaned$treat_cat == 1 | data_cleaned$treat_cat == 2)
### 2 and 4
t.test(data_cleaned$post_pos ~ data_cleaned$treat_cat, subset = data_cleaned$treat_cat == 2 | data_cleaned$treat_cat == 4 )
### 2 and 3
t.test(data_cleaned$post_pos ~ data_cleaned$treat_cat, subset = data_cleaned$treat_cat == 2 | data_cleaned$treat_cat == 3 )


data_man_pos <- data_cleaned %>% group_by(treat_cat) %>% summarise(m = mean(post_pos),
                                                               SE = sd(post_pos)/sqrt(length(post_pos)))


plot_man_pos <- ggplot(data_man_pos, aes(treat_cat,m,fill=as.factor(treat_cat))) + 
  geom_point(stat="identity", color="black",
             position=position_dodge())+
  geom_errorbar(aes(ymin=m-1.96*SE, ymax=m+1.96*SE), width=.5,
                position=position_dodge(.9))+
  scale_fill_manual(values=c("#B2BEB5",
                             "gray",
                             "darkgray",
                             "#71797E"))+
  theme_classic()+
  theme(legend.position = "none")+
  labs(title = "Manipulation Check for Mainstream Party Positions")+
  xlab("Treatment Category")+
  ylab("Mainstream Party's Position resembles AfD's position")+
  scale_x_continuous(
    breaks = c(1, 2, 3, 4),
    label = c("No accommodation", "Positional \n Accommodation", "Rhetorical \n Accommodation", "Full \n Accommodation"))+
  geom_signif(comparisons = list(c(2, 4)), annotations = "Not significant", y_position = 4.1)+
  geom_signif(comparisons = list(c(1, 3)), annotations = "Not significant", y_position = 3.7)+
  geom_signif(comparisons = list(c(1, 2)), annotations = "***", y_position = 3.5)+
  geom_signif(comparisons = list(c(3, 4)), annotations = "***", y_position = 3.5)+
  theme(text = element_text(size = 14))
  

### manuscript figure 4 
mani_plots <- grid.arrange(plot_man_pos, plot_man_rhet, ncol = 2)
ggsave("Plots/manipluation_plots_271024.png", mani_plots, width = 280, height = 150, device='png', units = "mm", dpi=300, scale = 1.3)

mani_lang_pref <- lm(post_lang ~ position*pref_directed + rhetoric*pref_directed + rhetoric*position*pref_directed, data = data_cleaned)
summary(mani_lang_pref) 

texreg(list(mani_lang_pref),
       file = "Tables/manipulation_check_pref_271024.tex",
       include.ci = FALSE,
       stars = c(0.001, 0.01, 0.05),
       custom.coef.map = list("(Intercept)" = "No accommodation (Intercept)",
                              "rhetoric" = "Rhetorical Acc.",
                              "position" = "Positional Acc.",
                              "position:rhetoric" = "Full Acc.",
                              "pref_direct" = "Respondent LR-Preferences",
                              "pref_directed:rhetoric" = "Rhetorical Acc. * Respondent LR-Preferences",
                              "position:pref_directed" = "Positional Acc. * Respondent LR-Preferences",
                              "position:pref_directed:rhetoric" = "Full Acc. * Respondent LR-Preferences"),
       booktabs = TRUE,
       dcolumn = FALSE,
       custom.note = "%stars.",
       digits = 2,
       label = "Manipulation Checks for Perceptions of Rhetoric conditional on Respondent LR-Preferences")


```

# HTEs by Past Vote

```{r vote HTEs,  include=TRUE, message = FALSE}

afd_fit <- lm(party_eval ~ position*pastVoteD + rhetoric*pastVoteD + position*rhetoric*pastVoteD + issue +party, data = data_cleaned)
summary(afd_fit)

### appendix table 6

texreg(list(afd_fit),
       file = "Tables/afd_model_271024.tex",
       include.ci = FALSE,
       stars = c(0.001, 0.01, 0.05),
       custom.coef.map = list("(Intercept)" = "No accommodation (Intercept)",
                              "rhetoric" = "Rhetorical Accommodation",
                              "position" = "Position Accommodation",
                              "position:rhetoric" = "Full Accommodation",
                              "pastVoteD" = "Past AfD Vote",
                              "pastVoteD:rhetoric" = "Past AfD Vote * Rhetorical Acc.",
                              "position:pastVoteD" = "Past AfD Vote * Positional Acc.",
                              "position:pastVoteD:rhetoric" = "Past AfD Vote * Full Acc."),
       booktabs = TRUE,
       dcolumn = FALSE,
       custom.model.names = c("Model"),
       custom.note = "%stars. Controlling for Issue and Mainstream Party",
       digits = 2,
       label = "Effects conditional on Past Voting for the AfD")

party_fit <- lm(party_eval ~ position*pastVote + rhetoric*pastVote + position*rhetoric*pastVote + issue, data = data_cleaned)
summary(party_fit)

texreg(list(party_fit),
       file = "Tables/pastVote_model.tex",
       include.ci = FALSE,
       single.row = TRUE,
       stars = c(0.001, 0.01, 0.05),
       custom.coef.map = list("(Intercept)" = "No accommodation (Intercept)",
                              "rhetoric" = "Rhetorical Accommodation",
                              "position" = "Positional Accommodation",
                              "position:rhetoric" = "Full Accommodation",
                              "pastVoteAfD" = "AfD",
                              "pastVoteCDU/CSU" = "CDU/CSU",
                              "pastVoteDie Gr\xfcnen" = "Die Grünen",
                              "pastVoteDie Linke" = "Die Linke",
                              "pastVoteDon't know" = "Don't know",
                              "pastVoteFDP" = "FDP",
                              "pastVoteIneligible" = "Ineligible",
                              "pastVoteNon-voter" = "Non-voters",
                              "pastVoteOthers" = "Others",
                              "pastVoteSPD" = "SPD",
                              "position:pastVoteCDU/CSU" = "Positional * CDU/CSU",
                              "position:pastVoteDie Gr\xfcnen" = "Positional * Die Gruenen",
                              "position:pastVoteDie Linke" = "Positional * Die Linke",
                              "position:pastVoteDon't know" = "Positional * Don't know",
                              "position:pastVoteFDP" = "Positional * FDP",
                              "position:pastVoteIneligible" = "Positional *Ineligible",
                              "position:pastVoteNon-voter" = "Positional * Non-voters",
                              "position:pastVoteOthers" = "Positional * Others",
                              "position:pastVoteSPD" = "Positional * SPD",
                              "pastVoteCDU/CSU:rhetoric" = "Rhetorical * CDU/CSU",
                              "pastVoteDie Gr\xfcnen:rhetoric" = "Rhetorical * Die Gruenen",
                              "pastVoteDie Linke:rhetoric" = "Rhetorical * Die Linke",
                              "pastVoteDon't know:rhetoric" = "Rhetorical * Don't know",
                              "pastVoteFDP:rhetoric" = "Rhetorical * FDP",
                              "pastVoteIneligible:rhetoric" = "Rhetorical *Ineligible",
                              "pastVoteNon-voter:rhetoric" = "Rhetorical * Non-voters",
                              "pastVoteOthers:rhetoric" = "Rhetorical * Others",
                              "pastVoteSPD:rhetoric" = "Rhetorical * SPD",
                              "position:pastVoteCDU/CSU:rhetoric" = "Full * CDU/CSU",
                              "position:pastVoteDie Gr\xfcnen:rhetoric" = "Full * Die Gruenen",
                              "position:pastVoteDie Linke:rhetoric" = "Full * Die Linke",
                              "position:pastVoteDon't know:rhetoric" = "Full * Don't know ",
                              "position:pastVoteFDP:rhetoric" = "Full * FDP ",
                              "position:pastVoteIneligible:rhetoric" = "Full *Ineligible ",
                              "position:pastVoteNon-voter:rhetoric" = "Full * Non-voters",
                              "position:pastVoteOthers:rhetoric" = "Full * Others",
                              "position:pastVoteSPD:rhetoric" = "Full * SPD"),
       booktabs = TRUE,
       dcolumn = FALSE,
       custom.model.names = c("Model"),
       custom.note = "%stars. Controlling for Issue and Mainstream Party",
       digits = 2,
       label = "Heterogeneity by Past Vote")

```

## HTEs by Party displayed in experiment

```{r party HTEs,  include=TRUE, message = FALSE}

mainstream_fit <- lm(party_eval ~ position + rhetoric + position*party +  rhetoric*party + position*rhetoric*party+ issue , data = data_cleaned)
summary(mainstream_fit)

## Appendix table 9
texreg(list(mainstream_fit),
       file = "Tables/parties_model_271024.tex",
       include.ci = FALSE,
       stars = c(0.001, 0.01, 0.05),
       custom.coef.map = list("(Intercept)" = "No accommodation (Intercept)",
                              "rhetoric" = "Rhetorical Accommodation",
                              "position" = "Positional Accommodation",
                              "position:rhetoric" = "Full Accommodation",
                              "partySPD" = "SPD (base: CDU/CSU)",
                              "rhetoric:partySPD" = "SPD (base: CDU/CSU) * Rhetorical Acc.",
                              "position:partySPD" = "SPD (base: CDU/CSU) * Positional Acc.",
                              "position:rhetoric:partySPD" = "SPD (base: CDU/CSU) * Full Acc."),
       booktabs = TRUE,
       dcolumn = FALSE,
       custom.model.names = c("Model"),
       custom.note = "%stars. Controlling for Issue",
       digits = 2,
       label = "Heterogeneity between Mainstream Parties")


data_main_spd <- data_cleaned %>% subset(party == "SPD") %>% group_by(treat_cat) %>% summarise(m = mean(party_eval), SE = sd(party_eval)/sqrt(length(party_eval)))

plot_main_SPD <- ggplot(data_main_spd, aes(treat_cat,m,fill=as.factor(treat_cat))) + 
  geom_point(stat="identity", color="black",
             position=position_dodge())+
  geom_errorbar(aes(ymin=m-1.96*SE, ymax=m+1.96*SE), width=.5,
                position=position_dodge(.9))+
  theme_classic()+
  theme(legend.position = "none")+
  labs(title = "SPD")+
  xlab("Treatment Category")+
  ylab("Propensity to Vote for Mainstream Party (-5 to 5)")+
  scale_x_continuous(
    breaks = c(1, 2, 3, 4),
    label = c("No accommodation", "Positional \n Accommodation", "Rhetorical \n Accommodation", "Full \n Accommodation"))+
  theme(text = element_text(size = 14))+
  geom_hline(yintercept =0, linetype = 2)+
  theme(text = element_text(size = 14))


data_main_cdu <- data_cleaned %>% subset(party == "CDU/CSU") %>% group_by(treat_cat) %>% summarise(m = mean(party_eval),
                                                                                               SE = sd(party_eval)/sqrt(length(party_eval)))

plot_main_CDU <- ggplot(data_main_cdu, aes(treat_cat,m,fill=as.factor(treat_cat))) + 
  geom_point(stat="identity", color="black",
             position=position_dodge())+
  geom_errorbar(aes(ymin=m-1.96*SE, ymax=m+1.96*SE), width=.5,
                position=position_dodge(.9))+
  theme_classic()+
  theme(legend.position = "none")+
  labs(title = "CDU/CSU")+
  xlab("Treatment Category")+
  ylab("Propensity to Vote for Mainstream Party (-5 to 5)")+
  scale_x_continuous(
    breaks = c(1, 2, 3, 4),
    label = c("No accommodation", "Positional \n Accommodation", "Rhetorical \n Accommodation", "Full \n Accommodation"))+
   theme(text = element_text(size = 14))+
  geom_hline(yintercept =0, linetype = 2)+
  theme(text = element_text(size = 14))

## appendix figure 7
party_plots <- grid.arrange(plot_main_CDU, plot_main_SPD, ncol = 2)
party_plots

ggsave("Plots/plot_main_parties_271024.png", party_plots, width = 250, height = 160, device='png', units = "mm", dpi=300, scale = 1.3)

```
## HTEs by Party displayed and Past Vote

```{r party supporter HTEs,  include=TRUE, message = FALSE}

data_SPD <- subset(data_cleaned, data_cleaned$party == "SPD")
data_CDU <- subset(data_cleaned, data_cleaned$party == "CDU/CSU")


party_fit1 <- lm(party_eval ~ position*pastVote + rhetoric*pastVote + position*rhetoric*pastVote + issue, data = data_SPD)
summary(party_fit1)

party_fit2 <- lm(party_eval ~ position*pastVote + rhetoric*pastVote + position*rhetoric*pastVote + issue , data = data_CDU)
summary(party_fit2)

## appendix table 11

texreg(list(party_fit1, party_fit2),
       file = "Tables/focalparty_model.tex",
       include.ci = FALSE,
       single.row = TRUE,
       stars = c(0.001, 0.01, 0.05),
       custom.coef.map = list("(Intercept)" = "No accommodation (Intercept)",
                              "rhetoric" = "Rhetorical Accommodation",
                              "position" = "Positional Accommodation",
                              "position:rhetoric" = "Full Accommodation",
                              "pastVoteAfD" = "AfD",
                              "pastVoteCDU/CSU" = "CDU/CSU",
                              "pastVoteDie Gr\xfcnen" = "Die Grünen",
                              "pastVoteDie Linke" = "Die Linke",
                              "pastVoteDon't know" = "Don't know",
                              "pastVoteFDP" = "FDP",
                              "pastVoteIneligible" = "Ineligible",
                              "pastVoteNon-voter" = "Non-voters",
                              "pastVoteOthers" = "Others",
                              "pastVoteSPD" = "SPD",
                              "position:pastVoteCDU/CSU" = "Positional * CDU/CSU",
                              "position:pastVoteDie Gr\xfcnen" = "Positional * Die Gruenen",
                              "position:pastVoteDie Linke" = "Positional * Die Linke",
                              "position:pastVoteDon't know" = "Positional * Don't know",
                              "position:pastVoteFDP" = "Positional * FDP",
                              "position:pastVoteIneligible" = "Positional *Ineligible",
                              "position:pastVoteNon-voter" = "Positional * Non-voters",
                              "position:pastVoteOthers" = "Positional * Others",
                              "position:pastVoteSPD" = "Positional * SPD",
                              "pastVoteCDU/CSU:rhetoric" = "Rhetorical * CDU/CSU",
                              "pastVoteDie Gr\xfcnen:rhetoric" = "Rhetorical * Die Gruenen",
                              "pastVoteDie Linke:rhetoric" = "Rhetorical * Die Linke",
                              "pastVoteDon't know:rhetoric" = "Rhetorical * Don't know",
                              "pastVoteFDP:rhetoric" = "Rhetorical * FDP",
                              "pastVoteIneligible:rhetoric" = "Rhetorical *Ineligible",
                              "pastVoteNon-voter:rhetoric" = "Rhetorical * Non-voters",
                              "pastVoteOthers:rhetoric" = "Rhetorical * Others",
                              "pastVoteSPD:rhetoric" = "Rhetorical * SPD",
                              "position:pastVoteCDU/CSU:rhetoric" = "Full * CDU/CSU",
                              "position:pastVoteDie Gr\xfcnen:rhetoric" = "Full * Die Gruenen",
                              "position:pastVoteDie Linke:rhetoric" = "Full * Die Linke",
                              "position:pastVoteDon't know:rhetoric" = "Full * Don't know ",
                              "position:pastVoteFDP:rhetoric" = "Full * FDP ",
                              "position:pastVoteIneligible:rhetoric" = "Full *Ineligible ",
                              "position:pastVoteNon-voter:rhetoric" = "Full * Non-voters",
                              "position:pastVoteOthers:rhetoric" = "Full * Others",
                              "position:pastVoteSPD:rhetoric" = "Full * SPD"),
       booktabs = TRUE,
       dcolumn = FALSE,
       custom.model.names = c("Focal Party: SPD", "Focal Party: CDU"),
       custom.note = "%stars. Controlling for Issue",
       digits = 2,
       label = "Heterogeneity by Past Vote for different Focal Parties")


```

# Moderation by Populist Attitudes

```{r HTEs Pop Atts, include=TRUE, message = FALSE}

pop_fit <- lm(party_eval ~ position*pop_att + rhetoric*pop_att +  position*rhetoric*pop_att + issue +party, data = data_cleaned)
summary(pop_fit) 


# table 7
texreg(list(pop_fit),
       file = "Tables/pop_att_model_271024.tex",
       include.ci = FALSE,
       stars = c(0.001, 0.01, 0.05),
       custom.coef.map = list("(Intercept)" = "No accommodation (Intercept)",
                              "rhetoric" = "Rhetorical Accommodation",
                              "position" = "Positional Accommodation",
                              "position:rhetoric" = "Full Accommodation",
                              "pop_att" = "Populist Attitudes",
                              "pop_att:rhetoric" = "Populist Attitudes * Rhetorical Acc.",
                              "position:pop_att" = "Populist Attitudes * Positional Acc.",
                              "position:pop_att:rhetoric" = "Populist Attitudes * Full Acc."),
       booktabs = TRUE,
       dcolumn = FALSE,
       custom.model.names = c("Model"),
       custom.note = "%stars. Controlling for Issue and Mainstream Party",
       digits = 2,
       label = "Effects conditional on Populist Attitudes among Voters")


```

# Moderation by Issues

```{r HTEs Issues, include=TRUE, message = FALSE}
issue_fit <- lm(party_eval ~ position + rhetoric + position*issue +  rhetoric*issue + rhetoric*issue*position + party , data = data_cleaned)
summary(issue_fit)

# appendix table 8
texreg(list(issue_fit),
       file = "Tables/issue_model.tex",
       include.ci = FALSE,
       stars = c(0.001, 0.01, 0.05),
       custom.coef.map = list("(Intercept)" = "No accommodation (Intercept)",
                              "rhetoric" = "Rhetorical Accommodation",
                              "position" = "Positional Accommodation",
                              "position:rhetoric" = "Full Accommodation",
                              "issueInflation" = "Inflation (base: Immigration)",
                              "issueUkraine" = "Ukraine (base: Immigration)",
                              "rhetoric:issueInflation" = "Inflation * Rhetorical Acc.",
                              "position:issueInflation" = "Inflation * Positional Acc.",
                              "position:rhetoric:issueInflation" = "Inflation * Full Acc.",
                              "rhetoric:issueUkraine" = "Ukraine * Rhetorical Acc.",
                              "position:issueUkraine" = "Ukraine * Positional Acc.",
                              "position:rhetoric:issueUkraine" = "Ukraine * Full Acc."),
       booktabs = TRUE,
       dcolumn = FALSE,
       custom.model.names = c("Model"),
       custom.note = "%stars. Controlling for Issue",
       digits = 2,
       label = "Heterogeneity between Issues (Reference Category: Immigration)")


data_main_inf <- data_cleaned %>% subset(issue == "Inflation") %>% group_by(treat_cat) %>% summarise(m = mean(party_eval),
                                                                                                     SE = sd(party_eval)/sqrt(length(party_eval)))

plot_main_inf <- ggplot(data_main_inf, aes(treat_cat,m,fill=as.factor(treat_cat))) + 
  geom_point(stat="identity", color="black",
             position=position_dodge())+
  geom_errorbar(aes(ymin=m-1.96*SE, ymax=m+1.96*SE), width=.5,
                position=position_dodge(.9))+
  theme_classic()+
  theme(legend.position = "none")+
  labs(title = "Inflation")+
  xlab("Treatment Category")+
  ylab("Propensity to Vote for Mainstream Party (-5 to 5)")+
  scale_x_continuous(
    breaks = c(1, 2, 3, 4),
    label = c("No accommodation", "Positional \n Acc.", "Rhetorical \n Acc.", "Full \n Acc."))+
  geom_signif(comparisons = list(c(1, 2)), annotations = "Not significant" , y_position = 0.05)+
  geom_signif(comparisons = list(c(1, 4)), annotations = "Not significant", y_position = 0.4)+
  geom_signif(comparisons = list(c(1, 3)), annotations = "Not significant", y_position = 0.2)+
  theme(text = element_text(size = 14))+
  geom_hline(yintercept =0, linetype = 2)+
  theme(text = element_text(size = 14))



data_main_ukr <- data_cleaned %>% subset(issue == "Ukraine") %>% group_by(treat_cat) %>% summarise(m = mean(party_eval),
                                                                                                   SE = sd(party_eval)/sqrt(length(party_eval)))

plot_main_ukr <- ggplot(data_main_ukr, aes(treat_cat,m,fill=as.factor(treat_cat))) + 
  geom_point(stat="identity", color="black",
             position=position_dodge())+
  geom_errorbar(aes(ymin=m-1.96*SE, ymax=m+1.96*SE), width=.5,
                position=position_dodge(.9))+
  theme_classic()+
  theme(legend.position = "none")+
  labs(title = "Ukraine")+
  xlab("Treatment Category")+
  ylab("Propensity to Vote for Mainstream Party (-5 to 5)")+
  scale_x_continuous(
    breaks = c(1, 2, 3, 4),
    label = c("No acc.", "Positional \n Acc.", "Rhetorical \n Acc.", "Full \n Acc."))+
  geom_signif(comparisons = list(c(1, 2)), annotations = "Not significant" , y_position = 0.05)+
  geom_signif(comparisons = list(c(1, 4)), annotations = "Not significant", y_position = 0.4)+
  geom_signif(comparisons = list(c(1, 3)), annotations = "Not significant", y_position = 0.2)+
  theme(text = element_text(size = 14))+
  geom_hline(yintercept =0, linetype = 2)+
  theme(text = element_text(size = 14))


data_main_immi <- data_cleaned %>% subset(issue == "Immigration") %>% group_by(treat_cat) %>% summarise(m = mean(party_eval),
                                                                                                        SE = sd(party_eval)/sqrt(length(party_eval)))

plot_main_immi <- ggplot(data_main_immi, aes(treat_cat,m,fill=as.factor(treat_cat))) + 
  geom_point(stat="identity", color="black",
             position=position_dodge())+
  geom_errorbar(aes(ymin=m-1.96*SE, ymax=m+1.96*SE), width=.5,
                position=position_dodge(.9))+
  theme_classic()+
  theme(legend.position = "none")+
  labs(title = "Immigration")+
  xlab("Treatment Category")+
  ylab("Propensity to Vote for Mainstream Party (-5 to 5)")+
  scale_x_continuous(
    breaks = c(1, 2, 3, 4),
    label = c("No accommodation", "Positional \n Acc.", "Rhetorical \n Acc.", "Full \n Acc."))+
  geom_signif(comparisons = list(c(1, 2)), annotations = "Not significant" , y_position = 0.05)+
  geom_signif(comparisons = list(c(1, 4)), annotations = "Not significant", y_position = 0.4)+
  geom_signif(comparisons = list(c(1, 3)), annotations = "Not significant", y_position = 0.2)+
  theme(text = element_text(size = 14))+
  geom_hline(yintercept =0, linetype = 2)+
  theme(text = element_text(size = 14))


## appendix figure 6
issue_plots <- grid.arrange(plot_main_immi, plot_main_inf, plot_main_ukr, ncol = 3)

ggsave("Plots/plot_main_issues_271024.png", issue_plots, width = 280, height = 160, device='png', units = "mm", dpi=300, scale = 1.3)


```

## Balance Test

```{r balance, include=TRUE, message = FALSE}

balance_fit <- lm(treat_cat ~ age + gender + nielsenregion + educ_neu + leftright_neu_shift + q_pol_interest + as.factor(pastVote), data = data_cleaned)
summary(balance_fit)

modelFrame <- data.frame(Coefficient = summary(balance_fit)$coef,
                          SE = summary(balance_fit)$coef[1:16, 2],
                          modelName = "Balance-Test")

modelFrame <- modelFrame[-1,]
modelFrame$Variable <- c("Age", "Gender", "Region",  "Education", "Left-Right", "Pol. Interest","PV: CDU/CSU", "PV: Die Grünen", "PV: Die Linke", "PV: Don't know", "PV: FDP", "PV: Ineligible", "PV: Non-Voter", "PV: Others", "PV: SPD")
colnames(modelFrame) <- c("Coefficient", "Coef-SE", "t.value", "p-value", "SE", "modelName", "Variable")

interval1 <- -qnorm((1-0.9)/2)  # 90% multiplier
interval2 <- -qnorm((1-0.95)/2)  # 95% multiplier
zp1 <- ggplot(modelFrame, aes())
zp1 <- zp1 + geom_hline(yintercept = 0, colour = gray(1/2), lty = 2)
zp1 <- zp1 + geom_linerange(aes(x = Variable, ymin = Coefficient - SE*interval1,
                                ymax = Coefficient + SE*interval1),
                            lwd = 1, position = position_dodge(width = 1/2))
zp1 <- zp1 + geom_pointrange(aes(x = Variable, y = Coefficient, ymin = Coefficient - SE*interval2,
                                 ymax = Coefficient + SE*interval2),
                             lwd = 1/2, position = position_dodge(width = 1/2),
                             shape = 21, fill = "WHITE")
zp1 <- zp1 + theme_classic()
zp1 <- zp1 + ggtitle("Balance Test")
zp1 <- zp1 +theme(axis.text.x = element_text(angle = 90,))
zp1 <- zp1 +  theme(text = element_text(size = 14))

print(zp1) 
## appendix figure 5
ggsave("Plots/balance_test_271024.png", zp1, width = 250, height = 180, device='png', units = "mm", dpi=300, scale = 1.3)

